package com.raos.springcloud;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 验证测试 spring cloud zookeeper配置中心实现
 *
 * @author raos
 * @emil 991207823@qq.com
 * @date 2022/9/5 23:04
 */
@RefreshScope // 它会在zk节点数据内容更新后将 @Value 注解字段进行刷新
@RestController
@SpringBootApplication
public class SpringCloudZkApp {

    public static void main(String[] args) {
        SpringApplication.run(SpringCloudZkApp.class, args);
    }

    @Resource
    private Environment environment;

    @RequestMapping("/spring-cloud-zk-config-env")
    public String configEnv() {
        return "[zk server env] " + this.environment.getProperty("name")
                + " " + this.environment.getProperty("hobby");
    }

    @Value("${name}")
    private String name;
    @Value("${hobby}")
    private String hobby;

    @RequestMapping("/spring-cloud-zk-config")
    public String configValue() {
        return "[zk server remote] " + this.name + " " + this.hobby;
    }

    @Autowired
    private DiscoveryClient discoveryClient;

    @RequestMapping("/sc-zk-discovery")
    public List<ServiceInstance> serviceUrl() {
        return discoveryClient.getInstances("spring-cloud-zookeeper");
    }

}
